import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/model/brand.dart';
import 'package:gzmall_app/common/toast_util.dart';

class GoodsSelectConditionWidget extends AnimatedWidget {
  Function onDismiss;
  Function onReset;
  Function onConfirm;
  Function onSelect;
  List<Brand> brands = [];
  int curBrandId = -1;
  GoodsSelectConditionWidget(
      {Key key,
      Animation<double> animation,
      this.onReset,
      this.onConfirm,
      this.onSelect,
      this.onDismiss,
      this.brands,
      this.curBrandId})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    List<Widget> conditionWidgets = [];
    for (var i = 0; i < brands.length; i++) {
      Brand _brand = brands[i];
      Widget cw = InkWell(
        onTap: () {
          onSelect(_brand.id);
        },
        child: Container(
          padding: const EdgeInsets.only(left: 15.0),
          alignment: Alignment.centerLeft,
          height: 36.0,
          color: _brand.id == curBrandId ? ColorCommon.primaryColor : Colors.white,
          child: Text(
            _brand.name,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color:
                  _brand.id == curBrandId ? Colors.white : ColorCommon.black1,
            ),
          ),
        ),
      );
      conditionWidgets.add(cw);
    }
    return Container(
        child: Column(children: <Widget>[
      Container(
          height: animation.value,
          color: ColorCommon.grey5,
          child: Column(
            children: <Widget>[
              Expanded(
                child: GridView.count(
                  padding: const EdgeInsets.only(top: 0.0),
                  shrinkWrap: true,
                  crossAxisCount: 2,
                  childAspectRatio: 4.0,
                  children: conditionWidgets,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 1.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          onReset();
                        },
                        child: Container(
                          height: 48.0,
                          alignment: Alignment.center,
                          color: Colors.white,
                          child: Text("重置"),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          if (curBrandId != null && curBrandId > 0) {
                            onConfirm(curBrandId);
                          } else {
                            ToastUtil.showMessageToast("请选择品牌");
                          }
                        },
                        child: Container(
                          height: 48.0,
                          alignment: Alignment.center,
                          color: ColorCommon.primaryColor,
                          child: Text(
                            "确定",
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          )),
      Expanded(
        child: GestureDetector(
            onTap: onDismiss,
            child: Container(
              color: ColorCommon.translucent3,
            )),
      ),
    ]));
  }
}
